﻿using System.IO;
using System.Web.UI.WebControls;

namespace WebUtilities
{
    public class WebDirectoryListing
    {
        public TreeNode List_all(string PhysicalRootPath,TreeView treeview,string VirtualRootPath)
        {
            TreeNode root_node = new TreeNode(PhysicalRootPath.Substring(PhysicalRootPath.LastIndexOf(Path.DirectorySeparatorChar)), VirtualRootPath,"",VirtualRootPath,"");
            treeview.Nodes.Add(root_node);

            string[] dirs = Directory.GetDirectories(PhysicalRootPath);
            foreach (string dir in dirs)
            {
                root_node.ChildNodes.Add(List_all(dir, treeview, VirtualRootPath + Path.DirectorySeparatorChar + dir.Substring(dir.LastIndexOf(Path.DirectorySeparatorChar))));
            }

            string[] files = Directory.GetFiles(PhysicalRootPath);
            foreach (string file in files)
            {
                TreeNode file_node = new TreeNode(Path.GetFileName(file), "", "",VirtualRootPath + Path.DirectorySeparatorChar + Path.GetFileName(file),"");
                root_node.ChildNodes.Add(file_node);
            }
            return root_node;
        }
    }
}
